package com.gavindon.android.adapters

import android.view.*
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.gavindon.android.R
import com.gavindon.android.entity.SmartEducationContentResp
import com.gavindon.android.entity.SmartLeftTreeResp
import com.gavindon.android.network.BASE_URL_IMG
import com.gavindon.android.utils.CourseFreeStatus
import com.gavindon.android.utils.FreeStatus
import com.gavindon.android.utils.click
import com.gavindon.android.utils.toFullVideoOrDetailPage
import com.gavindon.android.widget.FocusViewUtils

/**
 * description:
 * Created by liNan on  2022-2-6 15:18
 */


/*智慧教育左侧列表*/
class EducationLeftAdapter(private val lst: List<String>) :
    RecyclerView.Adapter<EducationLeftAdapter.ViewHolder>() {


    private var lastFocusIndex = -1
    private var lastFocusView: View? = null

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tv = itemView.findViewById<TextView>(R.id.tv_left)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context)
                .inflate(R.layout.adapter_education_left, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.tv.text = lst[position]
        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                lastFocusView?.isSelected = false
                if (lastFocusIndex != holder.absoluteAdapterPosition) {
                    focusListener?.invoke(position)
                }
                lastFocusIndex = holder.absoluteAdapterPosition
            }

        }
        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT && event.action == KeyEvent.ACTION_DOWN) {
                rightListener?.invoke(position)
                v.isSelected = true
                lastFocusView = v
                return@setOnKeyListener true
            }
            return@setOnKeyListener false
        }
    }

    override fun getItemCount() = lst.size

    //按下遥控器右键时的回调
    private var rightListener: ((Int) -> Unit)? = null
    fun setRightFocusChanged(e: (Int) -> Unit) {
        this.rightListener = e
    }

    //改变焦点的回调
    private var focusListener: ((Int) -> Unit)? = null
    fun setFocusChanged(e: (Int) -> Unit) {
        this.focusListener = e
    }
}


/*内容列表*/
class EducationAdapter : RecyclerView.Adapter<EducationAdapter.ViewHolder>() {
    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val iv = itemView.findViewById<ImageView>(R.id.ada_banner_iv)
        val tvContent = itemView.findViewById<TextView>(R.id.ada_content_tv)
        val ivIsFree = itemView.findViewById<ImageView>(R.id.ada_iv_free)
    }

    private var dataLst: MutableList<SmartEducationContentResp> = mutableListOf()

    fun clearAllData() {
        dataLst.clear()
        notifyDataSetChanged()
    }

    fun setNewData(datas: List<SmartEducationContentResp>?) {
        dataLst.clear()
        if (datas != null) {
            dataLst.addAll(datas)
        }
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.adapter_banner_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        val url = "${BASE_URL_IMG}/${dataLst[position].file.fileUrl}"
        Glide.with(holder.itemView)
            .load(url)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.iv)
        holder.tvContent.text = dataLst[position].courseName
        if (dataLst[position].isFree == FreeStatus.FREE.ordinal) {
            holder.itemView.setOnClickListener {
                val subjectType = dataLst[position].subjectType
                holder.itemView.context.toFullVideoOrDetailPage(
                    subjectType,
                    dataLst[position].courseId,
                    dataLst[position].courseName
                )
            }
        } else {

            holder.itemView.setOnClickListener {
                val subjectType = dataLst[position].subjectType
                holder.itemView.context.toFullVideoOrDetailPage(
                    subjectType,
                    dataLst[position].courseId,
                    dataLst[position].courseName
                )
            }

         /*   holder.itemView.click {
                val subjectType = dataLst[position].subjectType
                holder.itemView.context.toFullVideoOrDetailPage(
                    subjectType,
                    dataLst[position].courseId,
                    dataLst[position].courseName
                )
            }*/
        }


        holder.ivIsFree.setImageResource(if (dataLst[position].isFree == CourseFreeStatus.FREE.ordinal) R.mipmap.icon21 else R.mipmap.icon22)

        FocusViewUtils.scaleView(holder.itemView, 0)

        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && event.action == KeyEvent.ACTION_DOWN) {
                val finder = FocusFinder.getInstance()
                val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_LEFT)
                if (left == null) {
                    //根据左上右下定义顺序
                    dpadListener?.invoke(0)
                    return@setOnKeyListener true
                }
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP && event.action == KeyEvent.ACTION_DOWN) {
                val finder = FocusFinder.getInstance()
                val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                if (left == null) {
                    dpadListener?.invoke(1)
                    return@setOnKeyListener true
                }
            }
            return@setOnKeyListener false
        }
    }

    override fun getItemCount() = dataLst.size
    var dpadListener: ((Int) -> Unit)? = null
    fun setLeftOfUpFocusChanged(e: (Int) -> Unit) {
        this.dpadListener = e
    }
}

/*三级目录的水平tab*/
class HorizontalTabAdapter(layoutResId: Int, data: MutableList<SmartLeftTreeResp>?) :
    BaseQuickAdapter<SmartLeftTreeResp, BaseViewHolder>(layoutResId, data) {

    private var lastFocusView: View? = null
    override fun convert(holder: BaseViewHolder, item: SmartLeftTreeResp) {
        holder.setText(R.id.ada_tv_three_tab, item.subjectName)
        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            lastFocusView?.isSelected = false
            if (hasFocus) {
                focusListener?.invoke(data.indexOf(item))
            }
        }
        holder.itemView.setOnKeyListener { v, keyCode, event ->
            if (event.action == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                    val finder = FocusFinder.getInstance()
                    val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_LEFT)
                    if (left == null) {
                        //根据左上右下定义顺序
                        dpadListener?.invoke(0)
                        v.isSelected = true
                        lastFocusView = v
                        return@setOnKeyListener true
                    }
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    val finder = FocusFinder.getInstance()
                    val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                    if (left == null) {
                        dpadListener?.invoke(1)
                        v.isSelected = true
                        lastFocusView = v
                        return@setOnKeyListener true
                    }
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                    val finder = FocusFinder.getInstance()
                    val left = finder.findNextFocus(v.parent as ViewGroup, v, View.FOCUS_UP)
                    if (left == null) {
                        dpadListener?.invoke(3)
                        v.isSelected = true
                        lastFocusView = v
                        return@setOnKeyListener true
                    }
                }
            }

            return@setOnKeyListener false
        }
    }

    var focusListener: ((Int) -> Unit)? = null
    fun setViewFocusChanged(e: (Int) -> Unit) {
        this.focusListener = e
    }

    var dpadListener: ((Int) -> Unit)? = null
    fun setDpadChangedListener(e: (Int) -> Unit) {
        this.dpadListener = e
    }
}